package com.jk.touchtest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by kun on 2016/12/1.
 */

public class TouchView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
private Bitmap bitmap;
    private Matrix matrix;
    private float lastDis;
    private float lastDegress;
    private GestureDetector detector;
    private boolean isDouble;
    public TouchView(Context context) {
        this(context,null);
    }

    public TouchView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TouchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img1);
        matrix = new Matrix();
        detector = new GestureDetector(context, this);
        detector.setOnDoubleTapListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,matrix,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount()==2) {
            isDouble=true;
            float x1 = event.getX(0);
            float x2 = event.getX(1);
            float y1 = event.getY(0);
            float y2 = event.getY(1);
            float cx=(x1+x2)/2;
            float cy=(y1+y2)/2;
            float dis = (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
            float degrees = (float) Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_POINTER_DOWN:
                    lastDis=dis;
                    lastDegress=degrees;
                    break;
                case MotionEvent.ACTION_MOVE:
                    matrix.postScale(dis/lastDis,dis/lastDis,cx,cy);
                    matrix.postRotate(degrees-lastDegress,cx,cy);
                    lastDis=dis;
                    lastDegress=degrees;
                    break;
            }
        }else if(event.getPointerCount()==1){
            if (event.getAction()==MotionEvent.ACTION_DOWN){
                isDouble=false;
            }
            if (!isDouble){
            return  detector.onTouchEvent(event);
            }
        }
        invalidate();
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }
    //单击事件
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
    //滑动事件
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        matrix.postTranslate(-distanceX, -distanceY);
        invalidate();
        return false;
    }
    //长按事件
    @Override
    public void onLongPress(MotionEvent e) {

    }
    //抛动事件
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        matrix.postScale(1.2f, 1.2f, e.getX(), e.getY());
        invalidate();
        return false;
    }
}
